//
//  ViewController.m
//  纯代码封装
//
//  Created by hyl on 16/4/27.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "ViewController.h"
#import "AddViews.h"
#import "HYLShop.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *hub;
@property (weak, nonatomic) IBOutlet UIView *shopView;

@property (weak,nonatomic) UIButton *addbBtn;
@property (weak,nonatomic) UIButton *removeBtn;
@property (strong,nonatomic) NSArray *shops;
@end

@implementation ViewController

-(void)viewDidLoad{
    [super viewDidLoad];
    //   当加载完后添加一些按键
    //    加号按键
    self.addbBtn=[self addButton:@"add" hightNamed:@"add_highlighted" disabledName:@"add_disabled" tag:10 rect:CGRectMake(50, 40, 50, 50)];
    //    删除
    self.removeBtn=[self addButton:@"remove" hightNamed:@"remove_highlighted" disabledName:@"remove_disabled" tag:20 rect:CGRectMake(270, 40, 50, 50)];
    
}

-(void)click:(UIButton *)button{
    if (button.tag==10) {
        [self add];
        [self checkState];
    } else {
        [self remove];
        [self checkState];
    }
}
#pragma mark  - 添加按钮
-(UIButton *) addButton:(NSString *)name hightNamed:(NSString *)hightNamed disabledName:(NSString *)disabled tag:(int)tag rect:(CGRect)rect{
    UIButton *btn=[[UIButton alloc]init];
    [btn setBackgroundImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:hightNamed] forState:UIControlStateHighlighted];
    [btn setBackgroundImage:[UIImage imageNamed:disabled] forState:UIControlStateDisabled];
    btn.tag=tag;
    btn.frame=rect;
    [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    return btn;
}

#pragma mark - 添加
-(void)add{
    NSUInteger count=self.shopView.subviews.count;
    NSInteger cols=3;
    NSUInteger w=50;
    NSUInteger h=70;
    NSInteger colsMargin=(self.shopView.frame.size.width-w*cols)/(cols-1);
    NSInteger rowsMargin=20;
    //确定第几行第几列
    NSInteger col=count%cols;//
    NSInteger row=count/cols;
    
    UIView *view=[[UIView alloc]init];
    view.frame=CGRectMake(col*(w+colsMargin), row*(rowsMargin+h), w, h);
    
    HYLShop *shop=self.shops[count];
    //添加内容
    UIImageView *iconImage=[[UIImageView alloc]init];
    iconImage.image=[UIImage imageNamed:shop.icon];
    iconImage.frame=CGRectMake(0, 0, 50, 50);
    
    UILabel *lable=[[UILabel alloc]init];
    lable.text=shop.name;
    lable.textAlignment=NSTextAlignmentCenter ;
    lable.font=[UIFont systemFontOfSize:11];
    lable.frame=CGRectMake(0, 50, 50, 20);
    
    [view addSubview:iconImage];
    [view addSubview:lable];
    
    [self.shopView addSubview:view];
    
}

/**  重写property shops */
//不可使用self.shops会导致无限的循环
#pragma mark - 重写property shops
-(NSArray *)shops{
    if (_shops==nil) {
        NSArray *dicsArr=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"file" ofType:@"plist"]];
        NSMutableArray *shopsArr=[NSMutableArray array];
        for (NSDictionary *dic in dicsArr) {
            [shopsArr addObject:[HYLShop hYLShopWithDic:dic]];
        }
        _shops=shopsArr;
    }
    return _shops;
}

#pragma mark - 删除
/** 删除Uiview */
-(void)remove{
    [[self.shopView.subviews lastObject]removeFromSuperview];
}
#pragma mark - 检测按钮是否可用/包括hub
/**  检测按钮是否可用 */
-(void)checkState{
    self.addbBtn.enabled=self.shopView.subviews.count<self.shops.count;
    self.removeBtn.enabled=self.shopView.subviews.count>0;
    NSString *text=nil;
    if (!self.addbBtn.enabled) {
        text=@"不能再添加了!";
    } else if(!self.removeBtn.enabled) {
        text=@"不能再删除了!";
    }
    if(text==nil) return;
    self.hub.hidden=NO;
    self.hub.text=text;
    //hide hub
//    [self performSelector:@selector(hideHub) withObject:nil afterDelay:1.5];
//    [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(hideHub) userInfo:nil repeats:NO];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.hub.hidden=YES;
    });
}

#pragma mark - 隐藏hub
/** hide hub */
-(void) hideHub{
    self.hub.hidden=YES;
}
@end
